using UnityEngine;
using System.Collections;

public class MScrollViewItem : MControl
{
	public Texture view2;
	public MScrollView p;
	
	float worldX, worldY;
	
	bool bBeginEnable = false;
	
	public virtual void Start()
	{
        tx = view.width;
        ty = view.height;

        gameObject.transform.localScale = new Vector3(0, 1, 0);
        gameObject.transform.position = new Vector3(sx + tx / 2 + x, sy - ty / 2 - y, -depth);
		
        gameObject.renderer.material.mainTexture = view;
        gameObject.renderer.material.mainTextureOffset = new Vector2(1, 1);
        gameObject.renderer.material.mainTextureScale = new Vector2(-1, -1);
		
		worldX = gameObject.transform.position.x;
		worldY = gameObject.transform.position.y;
	}
	
	public void SetEnable(bool b)
	{
		gameObject.renderer.enabled = b;
	}
	
	public bool GetEnable()
	{
		return gameObject.renderer.enabled;
	}
	
	void Update()
	{
		base.Update();
		/*
		if( gameObject.renderer.enabled )
		{
			base.Update();
			Move(new Vector2(0,0));
		}
		*/
	}
	
	public virtual void OnEvent(Touch t)
	{
			Application.LoadLevel("MainScene");
		touchPhase = t.phase;
		if( t.phase == TouchPhase.Began )
		{
			OnDown(t);
            gameObject.renderer.material.mainTexture = view2;
			Application.LoadLevel("MainScene");
			//p.Down = true;
		}
		else if( t.phase == TouchPhase.Moved )
		{
			//p.OnEvent(t);
		}
		else if( t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled )
		{
			Application.LoadLevel("MainScene");
			if( OnUp(t) == 1 )
			{
				MUIManager.Instance().Event(Id, this);
			}
            gameObject.renderer.material.mainTexture = view;
			//p.Down = false;
		}
		//p.OnEvent(t);
	}
	/*
	public virtual void OnEvent(Touch t)
	{
		//touchPhase = t.phase;
		p.OnEvent(t);
		if( t.phase == TouchPhase.Began )
		{
			OnDown(t);
			//p.Down = true;
		}
		else if( t.phase == TouchPhase.Moved )
		{
			p.OnEvent(t);
		}
		else if( t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled )
		{
			if( OnUp(t) == 1 )
			{
				MUIManager.Instance().Event(Id, this);
			}
			p.Down = false;
		}
	}*/
	
	public virtual void OnCustomEvent(Touch t)
	{
		if( p.gameObject.renderer.enabled == false )
		{
			return;
		}
		MUIManager.Instance().Event(Id, this);
        //Application.LoadLevel("MainScene");
	}
	
	public void Move(Vector2 vec)
	{
		if( p.gameObject.renderer.enabled == false )
		{
			return;
		}
		float m = p.magin;
		Vector2 v1 = new Vector2(p.y+p.magin, p.y+p.tY-p.magin*4);
		Vector2 v2 = new Vector2(y - vec.y, y + tY - vec.y);
		
		if( (v1.x > v2.x && v1.x > v2.y) ||
			(v1.y < v2.x && v1.y < v2.y) )
		{
			gameObject.renderer.enabled = false;
			return;
		}
		
		gameObject.renderer.enabled = true;
		float dist = 0;
		
		int isTop = 0;
		
		if( v1.x > v2.x )
		{
			isTop = 1;
			dist = v1.x - v2.x;
		}
		else if( v1.y < v2.y )
		{
			isTop = 2;
			dist = v2.y - v1.y;
		}
		
		if( dist != 0 )
		{
			if( isTop == 1 )
			{
		        gameObject.renderer.material.mainTextureOffset = new Vector2(1, 1-dist/ty);
		        gameObject.renderer.material.mainTextureScale = new Vector2(-1, -1+dist/ty);
			}
			else
			{
		        gameObject.renderer.material.mainTextureOffset = new Vector2(1, 1);
		        gameObject.renderer.material.mainTextureScale = new Vector2(-1, -1+dist/ty);
			}
		
			if(ty-dist < 0 )
			{
	        	gameObject.transform.localScale = new Vector3(tx / 10, 1, 0);
			}
			else
			{
	        	gameObject.transform.localScale = new Vector3(tx / 10, 1, (ty-dist) / 10);
			}
		}
		else
		{
        	gameObject.transform.localScale = new Vector3(tx / 10, 1, ty / 10);
	        gameObject.renderer.material.mainTextureOffset = new Vector2(1, 1);
	        gameObject.renderer.material.mainTextureScale = new Vector2(-1, -1);
		}
		if( isTop == 0 )
			gameObject.transform.position = new Vector3(sx + tx / 2 + x, (sy - ty / 2 - y) + vec.y, -depth);
		else if( isTop == 1 )
			gameObject.transform.position = new Vector3(sx + tx / 2 + x, (sy - ty / 2 - p.y - p.magin) + dist/2, -depth);
		else if( isTop == 2 )
			gameObject.transform.position = new Vector3(sx + tx / 2 + x, (sy - ty / 2 - y + (vec.y + dist/2)), -depth);
	}
}

